package org.agilelang.plugin.load;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import org.agilelang.plugin.IPlugin;

public class LoadPlugins {

    public static void load(String path_xml, JMenu menuGenerate, JMenu menuDB, JMenu menuTool) {
        boolean cargados = ManagerLoadPlugins.loadPlugins(path_xml);
        if (cargados) {
            try {
                IPlugin[] avisadores = ManagerLoadPlugins.getPlugins();
                if (avisadores.length > 0) {
                    for (final IPlugin a : avisadores) {
                        if (a.getMenu().equals("generator")) {
                            JMenuItem MenuFile = new JMenuItem(a.getNamePlugin());
                            menuGenerate.add(MenuFile);
                            MenuFile.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
                                    System.out.println("Plugin: \t" + a.getClass().getCanonicalName());
                                    System.out.println("Mensaje:\t" + a.getNamePlugin());
                                    System.out.println();
                                    a.processPlugin();
                                }
                            });
                        } else if (a.getMenu().equals("db")) {
                            JMenuItem MenuFile = new JMenuItem(a.getNamePlugin());
                            menuDB.add(MenuFile);
                            MenuFile.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
                                    System.out.println("Plugin: \t" + a.getClass().getCanonicalName());
                                    System.out.println("Mensaje:\t" + a.getNamePlugin());
                                    System.out.println();
                                    a.processPlugin();
                                }
                            });
                        } else if (a.getMenu().equals("tool")) {
                            JMenuItem MenuFile = new JMenuItem(a.getNamePlugin());
                            menuTool.add(MenuFile);
                            MenuFile.addActionListener(new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
                                    System.out.println("Plugin: \t" + a.getClass().getCanonicalName());
                                    System.out.println("Mensaje:\t" + a.getNamePlugin());
                                    System.out.println();
                                    a.processPlugin();
                                }
                            });
                        }
                    }
                } else {
                    System.out.println("No se Encontraron Plugins");
                }
            } catch (Exception ex) {
                System.err.println("Excepcion: " + ex.getMessage());
                ex.printStackTrace();
            }
        } else {
            System.out.println("Plugins No Cargados");
        }
    }
}
